package edu.suda.xsap.controller;


import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.MemberDto;

import edu.suda.xsap.entity.Member;
import edu.suda.xsap.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/member")
@Slf4j
@Tags({@Tag(name = "会员管理", description = "会员管理")})
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Operation(summary = "查询会员1",description = "查询所有的会员信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/list")
    public Result list() {//查找全部
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<MemberDto> memberDtos = memberService.findAll();
            //
            return Result.success(memberDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @Operation(summary = "修改会员",description = "修改会员信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "修改失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @PutMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result update(@RequestBody Member member) {//修改
        try {
            memberService.update(member);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }
    @Operation(summary = "删除会员",description = "通过id删除会员")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "删除失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {//按id删除
        try {
            memberService.deleteById(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    @Operation(summary = "批量删除会员",description = "批量删除会员")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "删除失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @DeleteMapping("/deleteids")//批量删除
    public Result deleteSelected(@RequestParam("ids") Integer[] ids) {
        try {
            System.out.println(ids.toString());
            memberService.deleteSelected(ids);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.failure(e.getMessage(), e);
        }
    }


    @Operation(summary = "查询会员2",description = "按名字查询")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/search")
    public Result search(@RequestParam("name") String name) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<MemberDto> memberDtos = memberService.findByName(name);
            //
            return Result.success("查询成功",memberDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
    @Operation(summary = "查询会员3",description = "按id查询")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/searchbyid")
    public Result searchById(@RequestParam("id") Integer id) {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<MemberDto> memberDtos = memberService.findById(id);
            //
            return Result.success("查询成功",memberDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }


    @Operation(summary = "保存会员",description = "保存会员json")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @PostMapping("/add")
    public Result createMember(@RequestBody MemberDto memberDto) {
        Member member = Member.builder()
                .name(memberDto.getMemberName())
                .phone(memberDto.getPhoneNum())
                .birthday(memberDto.getBirthdate())
                .sex(memberDto.getGender())
                .note(memberDto.getRemark())
                .build();

        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            System.out.println(member);
            memberService.add(member);//保存数据到数据库中
            return Result.success();
        } catch (Exception e) {
            log.error("保存失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
}
